package com.ljn.service.imp;

import com.ljn.core.dto.Enterprise;
import com.ljn.core.dto.common.exceptions.UserIsExistedException;
import com.ljn.core.dto.common.utils.UUIDUtils;
import com.ljn.core.dto.common.utils.ency.MD5EncryptUtil;
import com.ljn.mapper.EnterpriseMapper;
import com.ljn.service.EnterpriseService;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by liujunnan on 2017/11/3.
 */
@Named
public class EnterpriseServiceImpl implements EnterpriseService {
    @Inject
    EnterpriseMapper enterpriseMapper;

    private Enterprise findByUsername(String userName) {
        return enterpriseMapper.findUserByUsername(userName);
    }

    @Transactional
    public Long register(Enterprise enterprise) {
        Enterprise result = this.findByUsername(enterprise.getUserName());
        if (result != null) {
            throw new UserIsExistedException("用户存在");
        } else {
            enterprise.setUserId(UUIDUtils.get32UUIDAsLowerCase());
            enterprise.setPassword(MD5EncryptUtil.encryptPassword(enterprise.getPassword(), enterprise.getUserId()));
            enterpriseMapper.insertUser(enterprise);
            enterpriseMapper.insertEnterprise(enterprise);
            return enterprise.getPhone();
        }
    }

    public Enterprise selectEnterprise(String userId) {
        Enterprise enterprise = enterpriseMapper.findEnterpriseById(userId);
        return enterprise;
    }



    @Override
    public boolean changeUserInfo(Enterprise enterprise) {
        return this.enterpriseMapper.updateByPrimaryKeySelective(enterprise) > 0 ? true:false;
    }
}
